﻿namespace KeRui.Upload
{
    using System;
    using System.IO;
    using System.Text;

    internal class Utilities
    {
        public static string ArraySummary(byte[] inArray)
        {
            return StreamSummary(new MemoryStream(inArray));
        }

        public static string EqualStream(Stream inS1, Stream inS2)
        {
            if (inS1.Length != inS2.Length)
            {
                return ("Length mismatch - " + inS1.Length.ToString() + " vs " + inS2.Length.ToString() + ".");
            }
            long position = inS1.Position;
            long num2 = inS2.Position;
            long length = inS1.Length;
            inS1.Position = inS2.Position = 0L;
            for (long i = 0L; i < length; i += 1L)
            {
                if (inS1.ReadByte() != inS2.ReadByte())
                {
                    return ("Difference detected at position " + i.ToString() + ".");
                }
            }
            inS1.Position = position;
            inS2.Position = num2;
            return "";
        }

        public static string FindFileExtension(string inPath)
        {
            int num = inPath.LastIndexOf('.');
            if (num >= 0)
            {
                return inPath.Substring(num + 1, (inPath.Length - num) - 1);
            }
            return "";
        }

        public static string FindFileName(string inPath)
        {
            int num = inPath.LastIndexOf('\\');
            if (num >= 0)
            {
                return inPath.Substring(num + 1, (inPath.Length - num) - 1);
            }
            return inPath;
        }

        public static string FindURLFileName(string inPath)
        {
            int num = inPath.LastIndexOf('/');
            if (num >= 0)
            {
                return inPath.Substring(num + 1, (inPath.Length - num) - 1);
            }
            return inPath;
        }

        public static string StreamSummary(Stream inStream)
        {
            return StreamSummary(inStream, 0x10);
        }

        public static string StreamSummary(Stream inStream, int theNum)
        {
            int position = (int) inStream.Position;
            string str = "";
            string str2 = "";
            str = string.Concat(new object[] { "At ", position.ToString(), " of ", inStream.Length, "." });
            inStream.Position = 0L;
            if (inStream.Length < theNum)
            {
                theNum = (int) inStream.Length;
            }
            for (int i = 1; i <= theNum; i++)
            {
                str2 = str2 + inStream.ReadByte().ToString();
                if (i != theNum)
                {
                    str2 = str2 + ", ";
                }
            }
            inStream.Position = position;
            if (theNum == inStream.Length)
            {
                return (str + " [" + str2 + "]");
            }
            return (str + " [" + str2 + ",...]");
        }

        public static string StreamToString(Stream inStream, int inCodePage)
        {
            long position = inStream.Position;
            byte[] buffer = new byte[inStream.Length];
            inStream.Read(buffer, 0, (int) inStream.Length);
            inStream.Position = position;
            return Encoding.GetEncoding(inCodePage).GetString(buffer);
        }
    }
}

